<?php
/**
 * @since 08.07.2011
 * @author Akulov Denis
 */
abstract class MVC_Controller_Base{


    /**
     * @var Container_Interface
     */
    private $_objContainer = null;


    /**
     * @var MVC_View_Interface
     */
    private $_objView = null;


    public function _accessDeniedHandler ($strMethodName, $arParams) {
        $this->_getView()->addView('Html_AccessDenied');
    }


    /**
     * @param Container_Interface $objContainer
     */
    public function _setContainer (Container_Interface $objContainer) {
        $this->_objContainer = $objContainer;
    }


    /**
     * @param MVC_View_Container_Interface $objViewContainer
     */
    public function _setView (MVC_View_Container_Interface $objViewContainer) {
        $this->_objView = $objViewContainer;
    }


    /**
     * @return string
     */
    public function _getResult () {
        return $this->_objView->render();
    }


    /**
     * @return Config_Interface
     */
    protected function _getConfig () {
        return $this->_getContainer()->get('Config');
    }


    /**
     * @return MVC_View_Element_Interface
     */
    protected function _addView ($strViewName) {
        return $this->_objView->addView($strViewName);
    }


    /**
     * @param string $strHookName
     * @param string $strHookFile
     * @return MVC_View_Hook_Element_Interface
     */
    protected function _addHookInsert ($strHookName, $strHookFile) {
        return $this->_getContainer()
                    ->get('View_Hooks')
                    ->addHookInsert($strHookName, $strHookFile);
    }


    /**
     * @param string $strHookName
     * @param string $strHookFile
     * @return MVC_View_Hook_Element_Interface
     */
    protected function _addHookAdd ($strHookName, $strHookFile) {
        return $this->_getContainer()
                    ->get('View_Hooks')
                    ->addHookAdd($strHookName, $strHookFile);
    }


    /**
     * @return mixed
     */
    protected function _SERVER_Item ($strOptionName) {
        return $this->_getContainer()
                    ->get('SERVER')
                    ->get($strOptionName);
    }


    /**
     * @return Container_OnlyRead_Interface
     */
    protected function _SERVER () {
        return $this->_getContainer()->get('SERVER');
    }


    /**
     * @return mixed
     */
    protected function _POST_Item ($strName) {
        return $this->_getContainer()
                    ->get('POST')
                    ->get($strName);
    }


    /**
     * @return Container_OnlyRead_Interface
     */
    protected function _POST () {
        return $this->_getContainer()->get('POST');
    }


    /**
     * @return mixed
     */
    protected function _GET_Item ($strName) {
        if ($this->_getContainer()->get('GET')->isExists($strName)) {
            return $this->_getContainer()
                        ->get('GET')
                        ->get($strName);
        }

        return false;
    }

    /**
     * @return Container_OnlyRead_Interface
     */
    protected function _GET () {
        return $this->_getContainer()->get('GET');
    }


    /**
     * @return Container_Interface
     */
    protected function _SESSION () {
        return $this->_getContainer()->get('SESSION');
    }


    protected function _SESSION_Item($strName) {
        if ($this->_getContainer()->get('SESSION')->isExists($strName)) {
            return $this->_getContainer()
                        ->get('SESSION')
                        ->get($strName);
        }

        return false;
    }


    protected function _getModel ($strModelName) {
        return $this->_getContainer()->get('MVC_ModelLoader')->getModel($strModelName);
    }


    protected function _redirect ($location) {
        if (substr($location, 0, 1) != '/') {
            $location = '/'.$location;
        }

        // Сохраним сессию перед переходом
        $_SESSION = $this->_SESSION()->getAsArray();

        $strDomain = $this->_getConfig()->getAttribute('BASE_URL', 'MAIN');
        $strProtocol = $this->_getConfig()->getAttribute('PROTOCOL', 'MAIN');
        header('Location:'.$strProtocol.$strDomain.$location);
        exit;
    }


    /**
     * @return Container_Interface
     */
    private function _getContainer () {
        return $this->_objContainer;
    }

}